﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using System.Runtime.CompilerServices;
using WebApi.Core.IService;
using WebApi.Core.Model;
using WebApi.Core.Common;
using WebApi.Core.Model.ResponseModels;
using Azure.Core;

namespace WebApi.Core.Api.Controllers
{ 
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthController : ControllerBase
    {
        private readonly IAuthService _authService;
        public AuthController(IAuthService authService)
        {
            _authService = authService;
        }

        [HttpPost]
        public async Task<ApiResponse<LoginResult>> Login(Users users)
        {
            try
            { 
                var result = await _authService.LoginAsync(users);
                return ApiResponse<LoginResult>.Success(result); 
            }
            catch (Exception ex)
            {
                return ApiResponse<LoginResult>.Fail(401,ex.Message);
            } 
        }

        [HttpPost]
        public async Task<ApiResponse> Register(Users users)
        {
            try
            {
                var result = await _authService.RegisterAsync(users); 
                return ApiResponse.Success();
            }
            catch (Exception ex)
            {
                return ApiResponse.Fail(500, "注册失败: " + ex.Message); 
            }
        }

        [HttpPost]
        public async Task<ApiResponse> Refresh(string accessToken, string refreshToken)
        {
            try
            { 
                var result = await _authService.RefreshTokenAsync(accessToken, refreshToken);
                return ApiResponse.Success();
            }
            catch (Exception ex)
            {
                return ApiResponse.Fail(500, "注册失败: " + ex.Message);
            }
        }
    }
}
